﻿/// <reference path="jquery-1.7.2.js" />
(function ($) {
    $.fn.Sparrow_dialog = function (options) {
        var defaults = {
            dialogId: "dialog1",
            titleHeight: 26,
            defaultIcon: "",
            typeDic: ["A:新增", "U:修改", "D:删除", "V:查看","P:打印"],
            canDrag: true,
            showMask: true
        };
        $.extend(defaults, options);

        var g = {
            initialize: function () {
                var dialog = "<div class='dialog-mask'></div><div class='dialog' id=" + defaults.dialogId + "><div class='dialog-title'><div class='dialog-title-icon'></div><div class='dialog-title-text' onselectstart='return false'>Window</div><div class='dialog-close'></div>" +
                    "</div><div class='dialog-content'><div id='dialog_pageloading'></div><iframe id='dlgFrame' frameborder='0' ></iframe></div></div>";
                if (!$("#" + defaults.dialogId).length > 0) {
                    $("body").append(dialog);
                }
                g.dialog = $("#" + defaults.dialogId, "body");
                g.dialogTitle = $(".dialog-title-text", g.dialog)
                g.dialogContent = $(".dialog-content", g.dialog)
                g.loading = $("#dialog_pageloading", g.dialog);
                g.iframe = $("iframe", g.dialog);
                g.moveContent = $("<div class='dialog-move'></div>");
                g.mask = $(".dialog-mask");

                g.closeButton = $(".dialog-close", g.dialog);
                //g.dialog.prepend(g.moveContent);
                if (!defaults.showMask) {
                    g.mask.hide();
                }
                if (defaults.canDrag) {
                    $("body").append(g.moveContent);
                    g.dialogTitle.css('cursor', 'move');
                    var moveX, moveY;
                    var leftKeyDown = false;
                    var titleArea = $(".dialog-title-text", g.dialog);
                    $(document).bind("selectstart", function () {
                        return false;
                    });
                    titleArea.on("mousedown", function (e) {
                        e.preventDefault();
                        if (e.which == 1) {
                            g.moveContent.css({ top: g.dialog.css("top"), left: g.dialog.css("left") });
                            g.moveContent.height(g.dialog.height()).width(g.dialog.width()).css('cursor', 'move').fadeTo(20, 0.2);
                            g.moveContent.show();
                            moveX = (e.pageX || e.screenX) - parseInt(g.dialog.css("left"));
                            moveY = (e.pageY || e.clientY) - parseInt(g.dialog.css("top"));
                            leftKeyDown = true;
                        }
                    });

                    $(document).on("mouseup", function (e) {
                        e.preventDefault();
                        if (leftKeyDown) {
                            g.moveContent.hide();
                            var newX = g.moveContent.css("left");
                            var newY = g.moveContent.css("top");
                            g.dialog.css({ "left": newX, "top": newY });
                            leftKeyDown = false;
                        }
                    }
                    );
                    $(document).on("mousemove", function (e) {
                        e.preventDefault();
                        if (leftKeyDown) {
                            var pX = (e.pageX || e.clientX) - moveX;
                            var pY = (e.pageY || e.clientY) - moveY;
                            g.moveContent.css({ top: pY, left: pX });
                        }
                    }
                    );
                }

                g.closeButton.hover(function () {
                    $(this).addClass("dialog-close-hover");
                }, function () {
                    $(this).removeClass("dialog-close-hover");
                }).click(function (event) {
                    if (defaults.canDrag) {
                        g.moveContent.hide();
                    }
                    g.iframe.attr("src", "");
                    g.dialog.hide();
                    if (defaults.showMask) {
                        g.mask.hide();
                    }
                });

            },
            onResize: function () {
                var w = g.dialog.width();
                var h = g.dialog.height();
                var l = g.dialog.parent().width() / 2 - w / 2;
                var t = g.dialog.parent().height() / 2 - h / 2 - 100;
                l = l < 0 ? 0 : l;
                t = t < 0 ? 0 : t;
                g.dialog.css({ "left": l, "top": t });
            },
            close: function () {
                if (defaults.canDrag) {
                    g.moveContent.hide();
                }
                g.dialog.hide();
                g.iframe.attr("src", "");
                if (defaults.showMask) {
                    g.mask.hide();
                }
            },
            open: function (url, type, title, w, h) {
                if (!g.dialog.find("#" + defaults.dialogId)) {
                    g.initialize();
                }
                var titleText;
                if (title == null || title == "") {
                    for (var i = 0; i < defaults.typeDic.length; i++) {
                        var splitItem = defaults.typeDic[i].split(":");
                        if (splitItem.length > 0 && splitItem[0] === type) {
                            title = splitItem[1];
                            break;
                        }
                    }
                }


                if (defaults.showMask) {
                    g.mask.show();
                }

                g.dialog.height(h).width(w);
                g.dialogTitle.text(title);
                g.dialogTitle.width(g.dialog.width() - 40);
                g.dialogContent.height(h - defaults.titleHeight - 1);
                g.iframe.height(h - defaults.titleHeight - 1);
                //g.loading.height(g.dialogContent.height());
                //g.loading.show();
                g.onResize();
                //                var d = new Date();
                //                var newUrl = url + "?" + d.getTime();
                g.iframe.attr("src", url);
                g.iframe.bind("load.tab", function () {
                    g.loading.hide();
                });

                g.dialog.show();
            }

        }
        g.initialize();
        $(window).resize(function () { g.onResize(); });
        return g;
    }
})(jQuery);